package com.example.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;

import android.os.Environment;

import android.widget.TextView;

import java.lang.reflect.Field;


public class SystemUtil {
    final static String TAG = "SystemUtil";
    public static final int REQUEST_CODE_REC_LICENSE = 200;    // 车牌识别
    private static final int PERMISSION_CODE = 102;    // 进入车牌扫描识别页面的权限码
    final public static String photoDir = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";  // 拍照路径
    final static String photoCacheTime = String.valueOf(System.currentTimeMillis() / (2 * 24 * 60 * 60 * 1000));    // 图片磁盘缓存时间，超过2天自动清空
    private static AlertDialog alertDialog;     // 弹窗


    /**
     * 字符串非空判断
     */
    public static boolean isInputCorrect(String Str) {
        return Str == null || Str.equals("") || Str.equals("null");
    }

    public static void showAlertDialog(Context context, String title, String msg) {
        if (isContextExisted(context)) {
            try {
                alertDialog = new AlertDialog.Builder(context).setTitle(title).setMessage(msg)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).show();
                // 设置按钮字体大小
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(24);
                // 设置标题字体
                Field mAlert = null;
                mAlert = AlertDialog.class.getDeclaredField("mAlert");
                mAlert.setAccessible(true);
                Object Controller = mAlert.get(alertDialog);
                //修改message信息
                Field mMessage = Controller.getClass().getDeclaredField("mMessageView");
                mMessage.setAccessible(true);
                TextView mMessageView = (TextView) mMessage.get(Controller);
                mMessageView.setPadding(20, 5, 5, 5);
                //title样式修改成色
                Field mTitle = Controller.getClass().getDeclaredField("mTitleView");
                mTitle.setAccessible(true);
                TextView mTitleView = (TextView) mTitle.get(Controller);
                if (mTitleView != null) {
                    mTitleView.setTextSize(24); // 标题
                    mMessageView.setTextSize(20);   // 文本
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private static AlertDialog customDialog;    // 自定义弹窗



    /**
     * 判断context是否存活
     */
    public static boolean isContextExisted(Context context) {
        if (context != null) {
            if (context instanceof Activity) {
                return !((Activity) context).isFinishing() && !((Activity) context).isDestroyed();
            } else return context instanceof Application;
        }
        return false;
    }


}
